﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Administration.Health;

namespace SPHealth.SharePoint.HealthRules.SP.Rules.Performance
{
    public class WarnULSLocation : SPHealthAnalysisRule
    {
        #region Locals
        private string ulsdriveletter = "";
        private string sharepointdriveletter = "";
        #endregion

        #region Constructor
        /* This rules does not require a constructor */
        #endregion

        #region Fields

        // the category to display this rule under
        public override SPHealthCategory Category
        {
            get { return SPHealthCategory.Performance; }
        }

        // the type or error to flag this rule as
        public override SPHealthCheckErrorLevel ErrorLevel
        {
            get { return SPHealthCheckErrorLevel.Warning; }
        }

        // the Explanation for the rule and what is not working
        public override string Explanation
        {
            get { return "The ULS logs should be written to a separate physical disk to improve system performance and throughput, see Configure diagnostic logging http://technet.microsoft.com/en-us/library/ee748656.aspx. Currently the ULS logs are being written to the '" + ulsdriveletter.ToUpper() + ":' drive along with the SharePoint installation files."; }
        }

        // how to fix the issue
        public override string Remedy
        {
            get { return "Assign the ULS logs to use a different physical hard disk than the one used by the SharePoint installation."; }
        }

        // the Summary for the rule
        public override string Summary
        {
            get { return "The ULS logs should not be written to the same physical disk that SharePoint is installed on."; }
        }

        public override SPHealthAnalysisRuleAutomaticExecutionParameters AutomaticExecutionParameters
        {
            get
            {
                SPHealthAnalysisRuleAutomaticExecutionParameters retval = new SPHealthAnalysisRuleAutomaticExecutionParameters();
                retval.Schedule = SPHealthCheckSchedule.Daily;
                retval.Scope = SPHealthCheckScope.Any;
                retval.ServiceType = typeof(SPTimerService);
                return retval;
            }
        }
        #endregion

        #region Methods

        // this method is called whenever the rule is executed
        public override SPHealthCheckStatus Check()
        {
            if (!SPFarm.Joined)
            {
                throw new InvalidOperationException();
            }
            
            string ulslocation = SPDiagnosticsService.Local.LogLocation;
            string sharepointlocation = Microsoft.SharePoint.Utilities.SPUtility.GetGenericSetupPath("");

            // replace any references to envronment variables
            ulslocation = common.ReplaceEnvironmentVariables(ulslocation);
            sharepointlocation = common.ReplaceEnvironmentVariables(sharepointlocation);

            // extract the drive letters
            ulsdriveletter = ulslocation.Substring(0, 1).ToLower();
            sharepointdriveletter = sharepointlocation.Substring(0, 1).ToLower();

            // determine is the check has passed or failed.
            if (sharepointdriveletter == ulsdriveletter)
                return SPHealthCheckStatus.Failed;
            else
                return SPHealthCheckStatus.Passed;
        }

        #endregion

    }
}
